//
//  WKPhotoView.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/9.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit


class WKPhotoViews: UIScrollView,WKPhotoViewDelegate {
    var images = [UIImage]()
    var maxCount:Int = 4
    weak var vc:BaseVC?
    private var views = [WKPhotoView]()

    
    convenience init(_ imgs:[UIImage] = [UIImage](), macC:Int = 4, thisVC:BaseVC? = nil) {
        self.init()
        self.maxCount = macC
        self.images = imgs
        self.vc = thisVC
        self.setInterFace()
    }
    
    func setInterFace()
    {
        
        if views.count > 0
        {
            for v in views {
                v.removeFromSuperview()
            }
            
            views.removeAll()
        }
        
        self.size = CGSize.init(width: screenWidth, height: 115)
        
        var x:CGFloat = 10
        let y:CGFloat = 5
        
        if images.count  > 0
        {
            for img in images {
                let v = WKPhotoView.init(img)
                v.origin = CGPoint.init(x: x, y: y)
                self.addSubview(v)
                x += v.width + 10
                v.delegate = self
                views.append(v)
            }
        }
        
        if images.count  < maxCount
        {
            let v = WKPhotoView.init(nil)
            v.origin = CGPoint.init(x: x, y: y)
            v.delegate = self
            self.addSubview(v)
            views.append(v)
            x += v.width + 10
        }
        self.contentSize = CGSize.init(width: x, height: 105)
    }
    
    func PhotoViewClick(WithType type: WKPhotoViewStats, photoView: WKPhotoView) {
        switch type {
        case .add:
            print("添加")
            let thisVC = WKPhotoPickerVC()
            thisVC.images = images
            thisVC.maxCount = 4
            thisVC.block = { (images) in
                self.images = images
                self.setInterFace()
            }
            vc?.present(thisVC, animated: true, completion: nil)
            
        case .look:
            print("查看")
            WKPhotoManager.shared.getSourceImage(WithImage: photoView.image!, block: { (img) in
                print("开始查看")
                
            })
        case .delete:
            print("删除")
            var index = 0
            for i in 0..<self.images.count {
                let img = self.images[i]
                if img.wk_isEqualImageTo(photoView.image!)
                {
                   index = self.images.index(of: img)!
                    break
                }
            }
            self.images.remove(at: index)
            self.setInterFace()
        }
    }
}


protocol WKPhotoViewDelegate {
    func PhotoViewClick(WithType type:WKPhotoViewStats, photoView:WKPhotoView)
}

enum WKPhotoViewStats {
    case add,look,delete
}

class WKPhotoView: UIView {

    var image:UIImage?
    var delegate:WKPhotoViewDelegate?
    private var IV = UIImageView()
    private var deleteBtn = UIButton.init(type: UIButtonType.custom)
    
    convenience init(_ img:UIImage? = nil) {
        self.init()
        self.setInterFace(WithImage: img)
    }
    
    func setInterFace(WithImage img:UIImage?)
    {
        self.isUserInteractionEnabled = true
        self.IV.isUserInteractionEnabled = true
//        self.backgroundColor = UIColor.brown
        self.image = img
        self.size = CGSize.init(width: 100, height: 100)
        IV.frame = CGRect.init(x: 0, y: 10, width: 90, height: 90)
        IV.image = self.image ?? UIImage.init(named: "tab-动态发布-添加图片")
        deleteBtn.setImage(UIImage.init(named: "删除"), for: UIControlState.normal)
        deleteBtn.frame = CGRect.init(x: 85, y: 0, width: 15, height: 15)
        self.addSubview(IV)
        self.addSubview(deleteBtn)
        deleteBtn.isHidden = self.image == nil
        deleteBtn.addTarget(self, action: #selector(deleteBtnClick), for: .touchUpInside)
        IV.addTarget(controlEvent: WKTapState.touchUpInside, target: self, handler: WKPhotoView.ivClick)
    }
    
    func deleteBtnClick()
    {
        delegate?.PhotoViewClick(WithType: .delete, photoView: self)
    }
    
    func ivClick(v:UIView)
    {
        delegate?.PhotoViewClick(WithType: image == nil ? WKPhotoViewStats.add : WKPhotoViewStats.look, photoView: self)
        
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
